<template>
  <el-dialog
      title="选择出参"
      v-if="showDom"
      v-el-drag-dialog
      :visible.sync="visible"
      width="720px"
      @closed="closed">
    <div class="">
      <div class="tag-group">
        <div class="tag-item-col"
             v-for="(item, index) in typeLists"
             :key="index">
          <div class="tag-item" @click="cliTag(item)" :title="item.className">
            <span>{{item.className}}</span>
            <!--<i class="el-icon-circle-plus-outline"></i>-->
          </div>
        </div>
      </div>
      <div class="selected-cont-group">
        <el-scrollbar style="height: 100%">
          <div class="selected-cont">
            <div class="selected-item" v-for="(item, index) in selectedDatas" :key="index">
              <span class="label-text"
                    :class="{'label-text-w100':item.type !='1'}"
                    @click="tapLabel(item,index)">{{ item.name }}</span>
              <input class="type-input" v-if="item.type =='1'" v-model="item.value" type="text">
              <i class="el-icon-circle-close" @click="delType(item, index)"></i>
            </div>
          </div>
        </el-scrollbar>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="hide">取 消</el-button>
      <el-button size="small" type="primary" @click="addType">确 定</el-button>
    </div>

    <StructureOutParamDialog ref="outParamDialog" @selected="selected"></StructureOutParamDialog>

  </el-dialog>
</template>

<script>
import {get_getPClass} from "@/apis/assembly/assembly";
import StructureOutParamDialog from "./StructureParamDialog";

export default {
  name: "ParamDialog",
  components: {StructureOutParamDialog},
  data() {
    return {
      funItem: null,
      funIndex: 0,
      showDom: false,
      visible: false,
      readonly: true,
      typeLists: [],
      selectedDatas: []
    }
  },
  methods: {
    show({item, index}) {
      this.visible = true;
      this.showDom = true;
      this.funItem = item
      this.funIndex = index
      this.getTypes()
      if (item.outParamsObj) {
        this.selectedDatas = JSON.parse(JSON.stringify(item.outParamsObj)) || []
      } else {
        this.selectedDatas = []
      }
    },
    hide() {
      this.visible = false
    },
    closed() {
      this.showDom = false
    },

    getTypes() {
      get_getPClass().then((res) => {
        this.typeLists = res.data.data;
      });
    },
    //选择类型
    cliTag(item) {
      this.selectedDatas = [{name: item.className, value: '',type:item.classType}];
      if (item.classType == 1) {
        this.$refs.outParamDialog.show({item: this.funItem, index: this.funIndex}, this.selectedDatas.length - 1, item)
      }
    },
    // 点击选中参数
    tapLabel(row, index){
      // this.$refs.outParamDialog.show({item: this.funItem, index: this.funIndex}, index, row)
    },
    //参数类型删除
    delType(val, index) {
      this.selectedDatas.splice(index, 1);
    },
    addType() {
      this.hide()
      this.$emit('selected', this.selectedDatas, this.funIndex)
    },
    selected(data, index) {
      this.selectedDatas[index].list = data
    }
  }
}
</script>

<style scoped lang="scss">

  .tag-group {
    display: flex;
    flex-wrap: wrap;
    margin-left: -5px;
    margin-right: -5px;

    .tag-item-col {
      padding-left: 5px;
      padding-right: 5px;
    }

    .tag-item {
      display: block;
      width: 100px;
      text-align: center;
      border: 1px solid #216892;
      line-height: 30px;
      cursor: pointer;
      position: relative;
      border-radius: 4px;
      color: #ffffff;
      margin-bottom: 10px;
      transition: all 0.3s;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;

      &:hover {
        background-color: #216892;
      }

      .el-icon-circle-close {
        position: absolute;
        right: 5px;
        top: 5px;
        cursor: pointer;
      }
    }
  }

  .selected-cont-group {
    border: 1px solid #216892;
    width: 100%;
    height: 150px;
    margin-top: 10px;
    border-radius: 4px;
    padding: 5px;
  }

  .selected-cont {
    display: flex;
    flex-wrap: wrap;

    .selected-item {
      width: calc(33.3333% - 10px);
      text-align: center;
      border: 1px solid #216892;
      background-color: #ececec;
      height: 30px;
      line-height: 30px;
      margin: 5px;
      cursor: pointer;
      position: relative;
      display: flex;
      align-items: center;
      padding-right: 20px;
      border-radius: 4px;
      overflow: hidden;

      .label-text {
        width: 50%;
        flex-shrink: 0;
        height: 100%;

        &.label-text-w100 {
          width: 100%;
        }
      }

      .type-input {
        width: 50%;
        height: 100%;
        flex-shrink: 0;
        border: none;
        padding: 0 10px;
      }

      .el-icon-circle-close {
        position: absolute;
        width: 14px;
        height: 14px;
        right: 5px;
        top: 0;
        bottom: 0;
        margin: auto 0;
        cursor: pointer;
      }
    }
  }
</style>